﻿myApp.controller("pageController", function ($scope, $http, $modal, modalService, $sce, $timeout, authService, $location,$rootScope) {
    $rootScope.waypointInit = $rootScope.waypointInit || false;
    $scope.data = {};
    $scope.isHome = false;

    var offset = $("#menu_bar").outerHeight() + 1;

    

    $('.menu_bar-waypoint').each(function (i) {
        var $el = $(this),
            animClassDown = $el.data('animateDown'),
            animClassUp = $el.data('animateUp');

        //console.log($el);
        $el.waypoint(function (direction) {
            var $head = $('#menu_bar');
            if (!$scope.isHome) {

            }
            else if (direction === 'down' && animClassDown) {
                $head.attr('class', 'menu_bar ' + animClassDown);
            }
            else if (direction === 'up' && animClassUp) {
                $head.attr('class', 'menu_bar ' + animClassUp);
            }
        }, {
            offset: function () {
                return offset;
            }
        });
    });
    $rootScope.$on('NavHomePage', function (event, data) {
        var $head = $('#menu_bar');
        $scope.isHome = true;
        $head.attr('class', 'menu_bar ' + 'menu_bar-hide');
        $.scrollTo('.hero_fullscreen', 1000, { easing: 'easeInOutExpo', 'axis': 'y' });
        console.log("Back Home Page");
        initWayPoint();
        resetWayPoint();
    });
    $rootScope.$on('NavOtherPage', function (event, data) {
        var $head = $('#menu_bar');
        $scope.isHome = false;
        $head.attr('class', 'menu_bar ' + 'menu_bar-show');
        $.scrollTo('.hero_fullscreen', 1000, { easing: 'easeInOutExpo', 'axis': 'y' });
        console.log("Navigate to Other Page");
    });

    function initWayPoint() {
        if (!$rootScope.waypointInit) {
            setTimeout(function () {
                $('#more_info').waypoint(function () {
                    setTimeout(function () { $('.content_anim1').addClass('animated fadeInUp') }, 0);
                    setTimeout(function () { $('.content_anim2').addClass('animated fadeInUp') }, 200);
                    setTimeout(function () { $('.content_anim3').addClass('animated fadeInUp') }, 400);
                    setTimeout(function () { $('.content_anim4').addClass('animated fadeInUp') }, 600);
                    setTimeout(function () { $('.content_anim5').addClass('animated fadeInUp') }, 800);
                }, { offset: '50%' });

                $('#contact').waypoint(function () {
                    setTimeout(function () { $('.content_anim29').addClass('animated fadeInUp') }, 0);
                    setTimeout(function () { $('.content_anim30').addClass('animated fadeInUp') }, 200);
                }, { offset: '50%' });
                $rootScope.waypointInit = true;
            }, 300);
        }
    }
    function resetWayPoint() {
        $('.content_anim29').removeClass('animated fadeInUp');
        $('.content_anim30').removeClass('animated fadeInUp');
    }

   function pageInit() {
        $scope.loader = { loading: true };
        var url = 'api/Page/GetSetting';
        $http.get(url).success(function (data) {
           $scope.data.setting =data;
           $scope.loader = { loading: false };
        })

        $rootScope.authentication = authService.authentication;

    }
    $scope.disclaimer = function () {

        //$scope.animationsEnabled = true;
        //var modalInstance = $modal.open({
        //    animation: $scope.animationsEnabled,
        //    templateUrl: 'view/Disclaimer.html',
        //    //controller: 'ModalInstanceCtrl',
        //    size: 'lg',


        //})

        var modalOptions = {
            closeButtonText: 'Cancel',
            actionButtonText: 'Ok',
            headerText: 'Disclaimer',
            bodyText: $sce.trustAsHtml($scope.data.setting.Disclaimer)
        };

        modalService.showModal({}, modalOptions);
    }
    $scope.getUsername = function () {
        if (authService) {
            if (authService.authentication) {
                if (authService.authentication.isAuth) {
                    if (authService.authentication.userName) {
                        if (authService.authentication.userName.length > 15) {
                            return authService.authentication.userName.substring(0, 15) + "...";
                        }
                        return authService.authentication.userName;
                    }
                }
            }
        }
        return "";
    }
    $scope.logout =function()
    {
        authService.logOut();
        $location.path('/Home');
    }

    pageInit();

});